package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/10/09 10:17
 **/
public class Day37_1_Num856_ScoreOfParentheses {
    class Solution {
        public int scoreOfParentheses(String s) {
            int sum = 0;
            int n = 0;
            for(int i = 0; i < s.length(); ++i){
                if(s.charAt(i) == '('){
                    if(n == 0) n = 1;
                    else n <<= 1;
                }else if(s.charAt(i) == ')'){
                    if(i > 0 && s.charAt(i-1) == '('){
                        sum += n;
                    }
                    n >>= 1;
                }
            }
            return sum;
        }
    }
}